library(stringr)
library(devtools)
source_url("https://gitee.com/ry2an/my_public_functions_r/raw/master/count_bar_generate_y.R")



split_calculate <- function(data = data.frame, 
                            name_vec_name = "", 
                            value_vec_name = "",
                            breaks = "",
                            other_vec = name_vec_name){
  select_vec  <- rep(T, dim(data)[1])
  for(i in 1:dim(data)[1]){
    if(grepl(x = data[i,name_vec_name], pattern = breaks)){
      select_vec[i] <- F
      id_pool <- str_split(data[i,name_vec_name], pattern = breaks)[[1]]
      weight_temp <- 1/length(id_pool)
      
      row_temp <- data[i,]
      this_value <- row_temp[,value_vec_name] * weight_temp
      for(j in 1:length(id_pool)){
        
        if(dim(data[data[,name_vec_name] == id_pool[j] & data[,other_vec] == row_temp[,other_vec],])[1] > 0){
          data[data[,name_vec_name] == id_pool[j], value_vec_name] <-
            data[data[,name_vec_name] == id_pool[j], value_vec_name] + this_value
        }else{
          row_temp[,name_vec_name] <- id_pool[j]
          row_temp[,value_vec_name] <- this_value
          data <- rbind(data, row_temp)
          select_vec <- c(select_vec, T)
        }
      }
    }
  }
  data <- data[select_vec,]
  return(data)
}

split_calculate2 <- function(data = data.frame, 
                            name_vec_name = "", 
                            value_vec_name = "",
                            breaks = "",
                            other_vec = name_vec_name){
  select_vec  <- rep(T, dim(data)[1])
  for(i in 1:dim(data)[1]){
    if(grepl(x = data[i,name_vec_name], pattern = breaks)){
      select_vec[i] <- F
      id_pool <- str_split(data[i,name_vec_name], pattern = breaks)[[1]]
      weight_temp <- 1/length(id_pool)
      
      row_temp <- data[i,]
      this_value <- row_temp[,value_vec_name] * weight_temp
      for(j in 1:length(id_pool)){
        
        # if(dim(data[data[,name_vec_name] == id_pool[j] & data[,other_vec] == row_temp[,other_vec],])[1] > 0){
        #   data[data[,name_vec_name] == id_pool[j], value_vec_name] <- 
        #     data[data[,name_vec_name] == id_pool[j], value_vec_name] + this_value
        # }else{
        row_temp[,name_vec_name] <- id_pool[j]
        row_temp[,value_vec_name] <- this_value
        data <- rbind(data, row_temp)
        select_vec <- c(select_vec, T)
        #}
        
        
      }
    }
  }
  data <- data[select_vec,]
  return(data)
}


#### TEST AREA ####
# other_vec <- c(as.character(11:20),20,20)
# value_vec <- rep(1,12)
# names <- c("a", "b", "c", "a-b", "b-c", "c-d", "a-c-e", "a-d", "b-c-e", "d-e","c", "d")
# data <- data.frame(names, value_vec, other_vec)
# 
# breaks = "-"
# value_vec_name <- "value_vec"
# name_vec_name <- "names"
# 
# split_calculate(data = data, value_vec_name = value_vec_name,
#                 name_vec_name = name_vec_name, breaks = breaks, other_vec = "other_vec")
# 
# 
# split_calculate2(data = data, value_vec_name = value_vec_name,
#                 name_vec_name = name_vec_name, breaks = breaks,  other_vec = "other_vec")

